<?php

class Default_Models_Category extends Libs_Model {

    private $cat_id;
    private $name;
    private $parent_id;

    public function setCatId($cat_id) {
        $this->cat_id = $cat_id;
    }

    public function getCatId() {
        return $this->cat_id;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }

    public function setParentId($parent_id) {
        $this->parent_id = $parent_id;
    }

    public function getParentId() {
        return $this->parent_id;
    }

    public function __construct() {
        parent::__construct();
    }

    protected function setValue($rows, $isKey = TRUE) {
        $pro = new Default_Models_Category();
        if ($isKey) {
            $pro->setCatId($rows['cat_id']);
        }

        $pro->setName($rows['name']);
        $pro->setParentId($rows['parent_id']);

        return $pro;
    }

    public function getParentCat() {
        $sql = "select * from tbl_categories where parent_id  =  0";
        $result = $this->db->query($sql);
        while ($rows = mysql_fetch_assoc($result)) {
            $out[] = $this->setValue($rows);
        }
        return $out;
    }

    public function getCatByParentId($id) {
        $sql = "select * from tbl_categories where parent_id = $id";
        $result = $this->db->query($sql);
        while ($rows = mysql_fetch_assoc($result)) {
            $out[] = $this->setValue($rows);
        }
        return $out;
    }

}
